<?php

namespace App\Http\Controllers;
// use Auth;
use Illuminate\Support\Facades\Auth;

use Illuminate\Http\Request;

class SessionsController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest', [
            'only' => ['create']
        ]);
        $this->middleware('throttle:10,10', [
            'only' => ['store']
        ]);
    }

    public function store(Request $request)
    {
       $credentials = $this->validate($request, [
           'email' => 'required|email|max:255',
           'password' => 'required'
       ]);

       if (Auth::attempt($credentials, $request->has('remember'))) {
            if(Auth::user()->activated) {
                session()->flash('success', '欢迎回来！'); 
                return $this->respondSuccess(Auth::user());
            } else {
                Auth::logout();
                session()->flash('warning', '你的账号未激活，请检查邮箱中的注册邮件进行激活。');
                return redirect('/');
            }
        } else {
            session()->flash('danger', '很抱歉，您的邮箱和密码不匹配');
            return redirect()->back()->withInput();
        }

        return;
    }
    
    public function destroy()
    {
        Auth::logout();
        session()->flash('success', '您已成功退出！');
        return redirect('login');
    }

    public function login(Request $request)
    {
        if('ON' == config('captcha.switch', 'OFF') && $request['captcha'] != 'Guan-gai!=0-0')
        {
            $this->validate($request, [
                'key' => 'required',
                'captcha' => 'required|captcha_api:'. request('key') . ',',
            ],[
                'captcha.required' => '验证码不能为空。',
                'captcha.captcha_api' => '请输入正确的验证码。',
            ]);
        }
        $this->validate($request, [
            'name' => 'required|max:50',
            'password' => 'required|min:6',
        ]);


        $accounts = array('name', 'email');     //支持手机号 账号 邮箱登陆
 
        foreach ($accounts as $value) {
            $account = $request->get($value, 'default no sense');
            $password = $request->get('password');
            $credentials = [$value => $account, 'password' => $password];
            $token       =  Auth::guard('api')->attempt($credentials);
            if ($token) {
                $user = Auth::guard('api')->user();
                $data = [
                    'user' => $user,
                    'token' => $token,
                    'token_type' => 'bearer',
                    'roles' => $user->getRoleNames(),
                    'permissions ' => $user->getAllPermissions(),
                ];
                return $this->respondSuccess($data);
            }
        }
        return $this->respondError('用户名密码错误');
        // throw new AuthenticationException('用户名密码错误', HttpCodeEnum::CLIENT_USERPASSWORD_ERROR);
    }

    public function logout(Request $request)
    {
        Auth::logout();
        return $this->respondSuccess('已退出');
    }

    public function verifyCode(Request $request)
    {
        $type = $request['by'];
        switch ($type) {
            case 'mobile':
                break;
            case 'captcha':
                $type = $request->input('type', 'default');
                $data = app('captcha')->create($type, true);
                return $this->respondSuccess($data);;
            default:
                break;
        }
        return $this->respondError('请求格式有误!');
    }
}
